package xj.model.uml;

import static xj.model.uml.UMLModelConstants.ATTR_COMMENT;
import static xj.model.uml.UMLModelConstants.ATTR_CONSTRAINT;
import static xj.model.uml.UMLModelConstants.ROLE_GUARD;
import static xj.model.uml.UMLModelConstants.ROLE_ACTION;
import static xj.model.uml.UMLModelConstants.ROLE_EVENT;

public class Transition 
  extends ConstrainedAssociationElement {

  public Transition() {
    this(null, null);
  }

  public Transition(State left, State right) {
    super(left, right);
  }

  public Event getEvent() { 
    return event;
  }

  public void setEvent(Event event) {
    this.event = event;
    setOwnedElement(ROLE_EVENT, event);
  }

  public String getEventName() { 
    if (event != null) { 
      return event.getName(); 
    } else { 
      return null;
    }
  }

  public void setEventName(String eventName) {
    if (event == null) { 
      setEvent(new Event(eventName));
    } else { 
      event.setName(eventName);
    }
  }

  public Expression getGuard() {
    return guard;
  }

  public void setGuard(Expression guard) {
    this.guard = guard;
    setOwnedElement(ROLE_GUARD, guard);
  }

  public String getGuardText() {
    if (guard != null) { 
      return guard.getBody();
    } else { 
      return null;
    }
  }

  public void setGuardText(String g) {
    if (guard == null) { 
      setGuard(new Expression(g));
    } else { 
      guard.setBody(g);
    }
  }

  public Action getAction() {
    return action;
  }

  public void setAction(Action a) {
    action = a;
    setOwnedElement(ROLE_ACTION, action);
  }

  public String getActionName() { 
    if (action != null) { 
      return action.getName();
    } else { 
      return null;
    }
  }

  public void setActionName(String actionName) {
    if (action == null) { 
      setAction(new Action(actionName));
    } else { 
      action.setName(actionName); 
    }
  }

  public String getActionBody() { 
    if (action != null) { 
      return action.getBody();
    } else { 
      return null;
    }
  }  

  public void setActionBody(String actionBody) {
    if (action == null) { 
      setAction(new Action(null, null, actionBody));
    } else { 
      action.setBody(actionBody); 
    }
  }

  @Override
  protected boolean asElement(String attrName) {
    if (attrName != null) {
      return (//attrName.equals(ATTR_GUARD) || 
	      attrName.equals(ATTR_COMMENT) || 
	      attrName.equals(ATTR_CONSTRAINT));
    }
    return false;
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    Transition t = (Transition) super.clone();
    if (event != null) { 
      t.setEvent((Event) event.clone());
    }
    if (guard != null) { 
      t.setGuard((Expression) guard.clone());
    }
    if (action != null) { 
      t.setAction((Action) action.clone());
    }
    return t;
  }

  protected Event event;

  protected Expression guard;

  protected Action action;

}
